<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<fmt:setBundle basename="ApplicationMessage" />
<fmt:setLocale value="zh_CN" />

<div class="modal-dialog demo2do-modal-dialog" style="width: 950px;">

  <div class="modal-content">

    <div class="modal-header arizona-breadcrumb no-bottom-border clearfix">

      <h2><i class="glyphicon glyphicon-fire" title=""></i>&nbsp;&nbsp;<span>联系人&nbsp;-&nbsp;新建</span></h2>

      <button type="button" class="close" data-dismiss="modal">&times;</button>

    </div>

    <form id="contact-form" class="form-horizontal" method="post" action="${ctx}/contacts" data-validate="true">

      <div class="modal-body">

        <h3 class="lengend"><span>联系人信息</span></h3>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">公司名称</label>
          <div class="pull-left width-30-percent">
            <input type="hidden" class="form-control select2-customer" name="customer" value="" data-validate="required">
          </div>
          
          <label class="col-xs-2 control-label">姓名</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="name" value="" data-validate="required">
          </div>
        </div>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">性别</label>
          <div class="pull-left width-30-percent">
            <select name="gender" class="select2 form-control" data-validate="required">
              <option></option>
              <c:forEach var="gender" items="${enums.gender}">
                <option value="${gender}"><fmt:message key="${gender.key}" /></option>
              </c:forEach>
            </select>
          </div>
          
          <label class="col-xs-2 control-label">职位</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="position" value="" data-validate="required">
          </div>
        </div>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">部门</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="department" value="" data-validate="required">
          </div>
        </div>

        <h3 class="lengend"><span>联系方式</span></h3>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">手机号码1</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="mobile1" value="">
          </div>

          <label class="col-xs-2 control-label">手机号码2</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="mobile2" value="">
          </div>
        </div>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">办公电话</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="workPhone" value="">
          </div>

          <label class="col-xs-2 control-label">家庭电话</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="familyPhone" value="">
          </div>
        </div>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">传真</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="fax" value="">
          </div>

          <label class="col-xs-2 control-label">电子邮件</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="email" value="">
          </div>
        </div>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">QQ</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="qq" value="">
          </div>

          <label class="col-xs-2 control-label">微信</label>
          <div class="pull-left width-30-percent">
            <input type="text" class="form-control" name="weixin" value="">
          </div>
        </div>

        <div class="form-group demo2do-form-group">
          <label class="col-xs-2 control-label">备注</label>
          <div class="pull-left width-30-percent">
            <textarea name="comment" class="form-control" cols="30" rows="3"></textarea>
          </div>
        </div>

      </div>

      <div class="modal-footer">
        <button type="submit" class="btn btn-success btn-shadow btn-shadow-success demo2do-btn">确定</button>&nbsp;&nbsp;&nbsp;&nbsp;
        <button type="button" class="btn btn-default btn-shadow btn-shadow-default demo2do-btn" data-dismiss="modal">取消</button>
      </div>

    </form>

  </div>
</div>

<script src="${ctx.resource}/js/bootstrap.validate.trigger.js"></script>
<script type="text/javascript">

  $(document).ready(function() {
    
    $('.select2').select2({
    	placeholder: '选择性别',
      minimumResultsForSearch: -1,
      allowClear: true
    });
    
    $('.select2-customer').select2({
      placeholder: '搜索客户',
      minimumInputLength: 2,
      allowClear: true,
      ajax: {
        url: '${ctx}/customers/search-signature',
        cache: true,
        dataType: 'json',
        data: function(term) {
          return {
            name: term
          };
        },
        results: function(data) {
          return { results: data };
        }
      },
      formatResult: function(customer) { return customer.name; },
      formatSelection: function(customer) { return customer.name; }
    });
    
    $('#contact-form').submit(function() {
    	
    	var $mobile1 = $('input[name="mobile1"]'),
    	  $mobile2 = $('input[name="mobile2"]'),
    	  $workPhone = $('input[name="workPhone"]');
    	
    	var mobile1 = $.bt_validate.fn['required'].call($mobile1, $mobile1.val()),
    	  mobile2 = $.bt_validate.fn['required'].call($mobile2, $mobile2.val()),
    	  workPhone = $.bt_validate.fn['required'].call($workPhone, $workPhone.val());
    	
    	if(!mobile1 && !mobile2 && !workPhone) {
    		if(!mobile1) $mobile1.parent().addClass('has-error').show_err_tooltip('至少填写一个联系方式！');
	      if(!mobile2) $mobile2.parent().addClass('has-error').show_err_tooltip('至少填写一个联系方式！');
	      if(!workPhone) $workPhone.parent().addClass('has-error').show_err_tooltip('至少填写一个联系方式！');
    	}
    	
    	return mobile1 || mobile2 || workPhone;
    	
    });
    
  });
	
</script>
